﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'UpdateStock')
	BEGIN
		DROP  Procedure  UpdateStock
	END

GO

CREATE Procedure UpdateStock
	@idBien int,
	@cantidad decimal(8,4)
AS

BEGIN

SET NOCOUNT ON

	BEGIN TRY

	UPDATE Bien SET StockActual = StockActual + @cantidad WHERE IdBien = @idBien
	
	DECLARE @stock decimal(8,4)
	SET @stock = ISNULL((SELECT StockActual FROM Bien WHERE IdBien = @idBien), 0)
	
	IF @stock < 1 
		BEGIN
			UPDATE Bien SET Estado = 0 WHERE IdBien = @idBien
		END
	ELSE
		BEGIN
			UPDATE Bien SET Estado = 1 WHERE IdBien = @idBien
		END 
	
	END TRY
	
	BEGIN CATCH
		EXEC RethrowError;
	END CATCH	

SET NOCOUNT OFF

END

GO


GRANT EXEC ON UpdateStock TO PUBLIC

GO


